package com.zzl.alligator.management.system.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.zzl.alligator.management.system.domain.SysUserAssist;
import com.zzl.alligator.management.system.mapper.SysUserAssistMapper;
import com.zzl.alligator.management.system.mapper.SysUserMapper;
import com.zzl.alligator.management.system.service.ISysUserAssistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;
/**
 * 用户辅助表(SysUserAssist)服务层
 *
 * @author zhouziliang
 * @since 2020-07-16
 */
@Service
@Slf4j
public class SysUserAssistService implements ISysUserAssistService {
    
    @Autowired
    private SysUserAssistMapper sysUserAssistMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 初始化， 将用户表数据初始化到用户辅助表
     */
    @PostConstruct
    public void init() {
        List<Long> ids = sysUserMapper.selectUserIdList();
        List<Long> assistIds = sysUserAssistMapper.selectUserIdList();
        Collection<Long> disIds = CollectionUtil.disjunction(ids, assistIds);
        if(CollectionUtil.isNotEmpty(disIds)) {
            int res = sysUserAssistMapper.batchInserUser(disIds);
            log.info("初始化{}条用户辅助表数据", res);
        }
    }
    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserAssist findSysUserAssistById(Long userId) {
        return sysUserAssistMapper.findSysUserAssistById(userId);
    }

   /**
     * 通过实体作为筛选条件查询
     *
     * @param sysUserAssist 实例对象
     * @return 对象列表
     */
    @Override
    public List<SysUserAssist> findSysUserAssistList(SysUserAssist sysUserAssist) {
        return sysUserAssistMapper.findSysUserAssistList(sysUserAssist);
    }

    /**
     * 新增数据
     *
     * @param sysUserAssist 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserAssist(SysUserAssist sysUserAssist) {
        return sysUserAssistMapper.insertSysUserAssist(sysUserAssist);
    }

    /**
     * 修改数据
     *
     * @param sysUserAssist 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserAssist(SysUserAssist sysUserAssist) {
        return sysUserAssistMapper.updateSysUserAssist(sysUserAssist);
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Long userId) {
        return sysUserAssistMapper.deleteById(userId);
    }

    /**
     * 初始化一条数据
     *
     * @param userId userId
     */
    @Override
    public void insertUser(Long userId) {
        SysUserAssist sysUserAssist = new SysUserAssist();
        sysUserAssist.setUserId(userId);
        sysUserAssistMapper.insertSysUserAssist(sysUserAssist);
    }

    /**
     * 用户全部已读
     *
     * @param userId 用户ID
     */
    @Override
    public int userPubNoticeClear(Long userId) {
        return sysUserAssistMapper.userPubNoticeClear(userId);
    }
}